package cn.lingyangwl.agile.food.core.service.impl;

import cn.lingyangwl.agile.food.core.assembly.GoodsSpecAssembly;
import cn.lingyangwl.agile.food.core.mapper.GoodsSpecMapper;
import cn.lingyangwl.agile.food.core.service.GoodsSpecService;
import cn.lingyangwl.agile.food.model.entity.GoodsSpec;
import cn.lingyangwl.agile.food.model.rqrs.goods.spec.GoodsSpecResp;
import cn.lingyangwl.agile.food.model.rqrs.goods.spec.GoodsSpecSaveReq;
import cn.lingyangwl.agile.food.model.rqrs.goods.spec.GoodsSpecUpdateReq;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品规格
 *
 * @author shenguangyang
 */
@Slf4j
@Service
public class GoodsSpecServiceImpl extends ServiceImpl<GoodsSpecMapper, GoodsSpec>
    implements IService<GoodsSpec>, GoodsSpecService {
    @Resource
    private GoodsSpecAssembly goodsSpecAssembly;
    
    @Override
    public List<GoodsSpecResp> listGoodsSpec() {
        LambdaQueryWrapper<GoodsSpec>  lqw = GoodsSpec.lqw()
                .orderByDesc(GoodsSpec::getCreatedAt);
    	return this.list(lqw).stream()
    	        .map(goodsSpecAssembly::toGoodsSpecResp).collect(Collectors.toList());
    }

    @Override
    public boolean saveGoodsSpec(GoodsSpecSaveReq req) {
        GoodsSpec entity = goodsSpecAssembly.toGoodsSpec(req);
    	return this.save(entity);
    }

    @Override
    public boolean updateGoodsSpec(GoodsSpecUpdateReq req) {
        GoodsSpec entity = goodsSpecAssembly.toGoodsSpec(req);
    	return this.updateById(entity);
    }

    @Override
    public boolean deleteGoodsSpec(Long id) {
    	return this.removeById(id);
    }
    
    @Override
    public boolean batchDeleteGoodsSpec(List<Long> ids) {
    	return this.removeByIds(ids);
    }
}
